﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Bestlife.ERP.DataAccess;

/// <summary>
/// Summary description for rubyTocrown
/// </summary>
/// 
namespace Bestlife.ERP.BusinessRules
{
    public class rubyTocrown
    {
        List<int> returnst = new List<int>();
        List<List<int>> insideReturnlist = new List<List<int>>();
        //int Num_agents = 0;
        //int Num_Ruby = 0;
        //int Num_diamond = 0;
        //int Num_SD = 0;
        //int Num_SED = 0;
        //int Num_CD = 0;
        Boolean upd = false;

        public rubyTocrown()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public Boolean checkRb_to_Crown(string user_id, double Tpv, double ppv, double cpv, List<List<int>> A_to_CD,string LVL)
        {
            int Num_agents = 0;
            int Num_Ruby = 0;
            int Num_diamond = 0;
            int Num_SD = 0;
            int Num_SED = 0;
            int Num_CD = 0;
            foreach (List<int> lire in A_to_CD)
            {
                int agent = int.Parse(lire[0].ToString());
                int ruby = int.Parse(lire[1].ToString());
                int diamand = int.Parse(lire[2].ToString());
                int sdiamand = int.Parse(lire[3].ToString());
                int sediamand = int.Parse(lire[4].ToString());
                int cdiamand = int.Parse(lire[5].ToString());

                if (agent > 0)
                {
                    // if (agent>=3)
                    //{
                    Num_agents++;
                    //}
                }
                if (ruby > 0)
                {
                    Num_Ruby++;
                }
                if (diamand > 0)
                {
                    Num_diamond++;
                }
                if (sdiamand > 0)
                {
                    Num_SD++;
                }
                if (sediamand > 0)
                {
                    Num_SED++;
                }
                if (cdiamand > 0)
                {
                    Num_CD++;
                }


            }



            #region Ruby

            if (((Tpv >= 1200)) || (Num_agents > 3 && cpv >= 1500 && ppv > 25) || (Num_Ruby > 3 && cpv >= 1500 && ppv > 25) || (Num_diamond > 3 && cpv >= 1500 && ppv > 25) || (Num_SD > 3 && cpv >= 1500 && ppv > 25) || (Num_SED > 3 && cpv >= 1500 && ppv > 25) || (Num_CD > 3 && cpv >= 1500 && ppv > 25) || (Num_agents > 2 && cpv >= 3200 && ppv > 25) || (Num_Ruby > 2 && cpv >= 3200 && ppv > 25) || (Num_diamond > 2 && cpv >= 3200 && ppv > 25) || (Num_SD > 2 && cpv >= 3200 && ppv > 25) || (Num_SED > 2 && cpv >= 3200 && ppv > 25) || (Num_CD > 2 && cpv >= 3200 && ppv > 25))// Agent
            {

                try
                {
                    String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                    Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                    String updStatus = "UPDATE Bonus  SET user_Status='6' WHERE User_id='" + user_id + "'";
                    upd = Bestlife.ERP.DataAccess.clsCon.AddEditDel(updStatus);
                    string Rank= "6";

                    #region Insert to Ruby Table 

                    String Ruby = "INSERT INTO Ruby (User_id,level_id,Agent_status)VALUES('" + user_id + "','" + LVL + "','" + Rank + "')";
                    Boolean chk = Bestlife.ERP.DataAccess.clsCon.AddEditDel(Ruby);
                    #endregion

                }


                catch
                {

                }


                finally
                {
                    clsCon.conn_new.Close();

                }


            }


            #endregion

            #region Diamond

            if (((Tpv >= 4800)) || (Num_Ruby > 3 && cpv >= 12000) || (Num_diamond > 3 && cpv >= 12000) || (Num_SD > 3 && cpv >= 12000) || (Num_SED > 3 && cpv >= 12000) || (Num_CD > 3 && cpv >= 12000) || (Num_Ruby > 2 && cpv >= 6000) || (Num_diamond > 2 && cpv >= 6000) || (Num_SD > 2 && cpv >= 6000) || (Num_SED > 2 && cpv >= 6000) || (Num_CD > 2 && cpv >= 6000))// Agent
            {

                try
                {
                    String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                    Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                    String updStatus = "UPDATE Bonus  SET user_Status='7' WHERE User_id='" + user_id + "'";
                    upd = Bestlife.ERP.DataAccess.clsCon.AddEditDel(updStatus);
                    string Rank = "7";
                    #region Insert to Diamand Table 

                    String Diamond = "INSERT INTO Diamond (User_id,level_id,Agent_status)VALUES('" + user_id + "','" + LVL + "','" + Rank + "')";
                    Boolean chk = Bestlife.ERP.DataAccess.clsCon.AddEditDel(Diamond);
                    #endregion

                }


                catch
                {

                }


                finally
                {
                    clsCon.conn_new.Close();

                }

            }


            // }   // diamond  

            #endregion

            #region Senior Diamond





            if ((Tpv >= 20000) || (Num_diamond >= 1 && Num_Ruby >= 6 && cpv >= 25000) || (Num_diamond >= 2 && Num_Ruby >= 4 && cpv >= 25000) || (Num_diamond > 2 && cpv >= 50000) || (Num_diamond >= 3 && cpv >= 25000))   // S diamond
            {

                try
                {
                    String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                    Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                    String updStatus = "UPDATE Bonus  SET user_Status='8' WHERE User_id='" + user_id + "'";
                    upd = Bestlife.ERP.DataAccess.clsCon.AddEditDel(updStatus);
                }


                catch
                {

                }


                finally
                {
                    clsCon.conn_new.Close();

                }

            }




            #endregion

            #region Senior Executive Diamond


            if ((Tpv >= 80000) || (Num_SD >= 1 && Num_diamond >= 6 && cpv >= 100000) || (Num_SD >= 2 && Num_diamond >= 4 && cpv >= 100000) || (Num_SD > 2 && cpv >= 200000) || (Num_SD >= 3 && cpv >= 100000))   // SE diamond
            {

                try
                {
                    String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                    Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                    String updStatus = "UPDATE Bonus  SET user_Status='9' WHERE User_id='" + user_id + "'";
                    upd = Bestlife.ERP.DataAccess.clsCon.AddEditDel(updStatus);
                }


                catch
                {

                }


                finally
                {
                    clsCon.conn_new.Close();

                }

            }




            #endregion

            #region Crown Diamond


            if ((Tpv >= 32000) || (Num_SED >= 3 && cpv >= 400000) || (Num_SED >= 2 && cpv >= 800000) || (Num_SD >= 4 && Num_SED >= 2 && cpv >= 400000) || (Num_SED >= 1 && Num_SD >= 6 && cpv >= 400000))   // S diamond
            {

                try
                {
                    String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                    Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                    String updStatus = "UPDATE Bonus  SET user_Status='10' WHERE User_id='" + user_id + "'";
                    upd = Bestlife.ERP.DataAccess.clsCon.AddEditDel(updStatus);
                }


                catch
                {

                }


                finally
                {
                    clsCon.conn_new.Close();

                }

            }


            A_to_CD.Clear();//
            return upd;

        }
            #endregion



           public Boolean returntoBrake(int agent,int ruby,int diamand,int sdiamand,int sediamand,int cdiamand,string Lvl_status)
           {
               Boolean rtnbrake=false;

               if (Lvl_status.Equals("5"))
               {

                   if ((agent >= 3) || (ruby >= 3) || (diamand >= 3) || (sdiamand >= 3) || (sediamand >= 3) || (cdiamand >= 3))
                   {
                       rtnbrake = true;
                   }

               }
               else if (Lvl_status.Equals("6"))
               {
                   if ((ruby >= 3) || (diamand >= 3) || (sdiamand >= 3) || (sediamand >= 3) || (cdiamand >= 3))
                   {
                       rtnbrake = true;
                   }

               }
               else if (Lvl_status.Equals("7"))
               {
                   if ((diamand >= 3) || (sdiamand >= 3) || (sediamand >= 3) || (cdiamand >= 3) || (diamand >= 2 && ruby >= 4) || (sdiamand >= 2 && ruby >= 4) || (sediamand >= 2 && ruby >= 4) || (cdiamand >= 2 && ruby >= 4) || (diamand >= 2 && diamand >= 4) || (diamand >= 2 && sdiamand >= 4) || (diamand >= 2 && sediamand >= 4) || (diamand >= 2 && cdiamand >= 4) || (sdiamand >= 2 && diamand >= 4) || (sdiamand >= 2 && sdiamand >= 4) || (sdiamand >= 2 && sediamand >= 4) || (sdiamand >= 2 && cdiamand >= 4) || (diamand >= 2 && cdiamand >= 4) || (sediamand >= 2 && diamand >= 4) || (sediamand >= 2 && sdiamand >= 4) || (sediamand >= 2 && sediamand >= 4) || (sediamand >= 2 && cdiamand >= 4) || (cdiamand >= 2 && diamand >= 4) || (cdiamand >= 2 && sdiamand >= 4) || (cdiamand >= 2 && sediamand >= 4) || (cdiamand >= 2 && cdiamand >= 4) || (diamand >= 1 && ruby >= 6) || (sediamand >= 1 && ruby >= 6) || (cdiamand >= 1 && ruby >= 6) || (diamand >= 1 && diamand >= 6) || (diamand >= 1 && sdiamand >= 6) || (diamand >= 1 && sediamand >= 6) || (diamand >= 1 && cdiamand >= 6) || (sediamand >= 1 && diamand >= 6) || (sediamand >= 1 && sdiamand >= 6) || (sediamand >= 1 && sediamand >= 6) || (sediamand >= 1 && cdiamand >= 6) || (cdiamand >= 1 && diamand >= 6) || (cdiamand >= 1 && sdiamand >= 6) || (cdiamand >= 1 && sediamand >= 6) || (cdiamand >= 1 && cdiamand >= 6))
                   {
                       rtnbrake = true;
                   }

               }
               else if (Lvl_status.Equals("8"))
               {
                   if ((sdiamand >= 3) || (sdiamand >= 2) || (diamand >= 3) ||(diamand >=2))   // S diamond
                   {
                       rtnbrake = true;
                   }
               }
               else if (Lvl_status.Equals("9"))
               {


               }
               else if (Lvl_status.Equals("10"))
               {


               }
               
               return rtnbrake;
           }
        
    }
}